<?php
namespace App\Repositories\User;

use App\Exceptions\OrdinaryException;
use App\Models\User\Qualification;
use App\Repositories\BaseRepository;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class QualificationRepository extends BaseRepository {

    public function model()
    {
        // TODO: Implement model() method.
        return Qualification::class;
    }

    /**
     * 获取认证资料
     *
     * @author zx
     * @date 2019-01-25
     * @param int $uid 用户ID
     * @return Array|Boolean
     */
    public function getQualificatioin(int $uid)
    {
        try{
            $res = $this->where("user_id", $uid)
                ->where("is_delete", 0)
                ->first();
            return $res->toArray();
        } catch (ModelNotFoundException $exception) {
            return false;
        }
    }

    /**
     * 添加|编辑用户认证资料
     *
     * @author zx
     * @date 2019-01-25
     * @param array $data 数据包
     *
     */
    public function addAuth(array $data)
    {
        try{
            $res = $this->where("user_id", $data['user_id'])
                ->where("is_delete", 0)
                ->first();
            $res = $res->update($data);
            if ( $res ) return $res->toArray();
            return false;
        } catch (ModelNotFoundException $exception) {
            $res = $this->create($data);
            if ( $res ) return $res->toArray();
            return false;
        }
    }

    protected function serialization(array $attributes)
    {
        // TODO: Implement serialization() method.
    }

}